import bianliang from "./bianliang.js";
import Boom from "./Boom.js"; //导入爆炸的对象

class Enemy {
	constructor() {
		var temp = parseInt(Math.random() * 100); // 我现在产生一个0~100之间的随机数
		if (temp >= 95) {
			this.type = 2;
			this.img = bianliang.resObj[5];
			this.speed = 3;
			this.life = 10;
			this.score = 100; 
		} else if (temp >= 85) {
			this.type = 1;
			this.img = bianliang.resObj[4];
			this.speed = 6;
			this.life = 3;
			this.score = 50; 
		} else {
			this.type = 0;
			this.img = bianliang.resObj[3];
			this.speed = 15;
			this.life = 1;
			this.score = 10; 
		}

		this.x = parseInt(Math.random() * 480);
		this.width = this.img.width;
		this.height = this.img.height;
		if (this.x > 480 - this.width) {
			this.x = 480 - this.width;
		}
		this.y = this.height * (-1);

	}
	move() {
		this.y += this.speed;
		if (this.y >= 850) {
			var index = bianliang.enemyList.indexOf(this);
			bianliang.enemyList.splice(index, 1);
		}
	}

	draw(ctx) {
		this.move();
		ctx.drawImage(this.img, this.x, this.y, this.width, this.height);
	}

	isDie() {
		if (this.life <= 0) {
			var b = new Boom(this.x, this.y, this.type);
			bianliang.boomList.push(b); 
			bianliang.score += this.score;
			var index = bianliang.enemyList.indexOf(this);
			bianliang.enemyList.splice(index, 1);

		} else {
			if (this.type == 1) {
				this.img = bianliang.resObj[8];
			} else if (this.type == 2) {
				this.img = bianliang.resObj[9];
			}
		}
	}
}

export default Enemy;
